﻿using RuoVea.Entity;
using Mapster;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using RuoVea.Autowired;
using RuoVea.ExSqlSugar;
using System.IO;
using RuoVea.ExIdGen;
using Microsoft.AspNetCore.Mvc;
using RuoVea.Service;
using MiniExcelLibs;
namespace RuoVea.Api.Controllers
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class SysUserController : ApiBaseController
    { 
        private readonly ISysUserService _sysUserService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysUserService"></param>
        public SysUserController(ISysUserService sysUserService)
        {
            _sysUserService = sysUserService;
        }

        /// <summary>
        /// 分页查询用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/sysUser/page")]
        public async Task<dynamic> QueryUserPageList([FromQuery] UserInput input)
        {
            return await _sysUserService.QueryUserPageList(  input);
        }

        [HttpGet("list")]
        public async Task<dynamic> List([FromQuery] UserInput input)
        {
            return await _sysUserService.List(input);
        }
       
        /// <summary>
        /// 增加用户       
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/add")]
        public async Task AddUser(AddUserInput input)
        {
              await _sysUserService.AddUser(input);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/delete")]
        public async Task DeleteUser(DeleteUserInput input)
        {
              await _sysUserService.DeleteUser(input);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/edit")]
        public async Task UpdateUser(UpdateUserInput input)
        {
              await _sysUserService.UpdateUser(input);
        }

        /// <summary>
        /// 查看用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/sysUser/detail")]
        public async Task<dynamic> GetUser([FromQuery] QueryUserInput input)
        {
            return await _sysUserService.GetUser(input);
        }

        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/changeStatus")]
        public async Task ChangeUserStatus(UpdateUserInput input)
        {
              await _sysUserService.ChangeUserStatus(input);
        }

        /// <summary>
        /// 授权用户角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/grantRole")]
        public async Task GrantUserRole(UpdateUserInput input)
        {
              await _sysUserService.GrantUserRole(input);
        }
 

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/updateInfo")]
        public async Task UpdateUserInfo(UpdateUserInput input)
        {
              await _sysUserService.UpdateUserInfo(input);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/updatePwd")]
        public async Task UpdateUserPwd(ChangePasswordUserInput input)
        {
              await _sysUserService.UpdateUserPwd(input);
        }

        /// <summary>
        /// 获取用户拥有角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/sysUser/ownRole")]
        public async Task<dynamic> GetUserOwnRole([FromQuery] QueryUserInput input)
        {
            return await _sysUserService.GetUserOwnRole(input);
        }
 

        /// <summary>
        /// 重置用户密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/resetPwd")]
        public async Task ResetUserPwd(QueryUserInput input)
        {
              await _sysUserService.ResetUserPwd(input);
        }

        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/sysUser/updateAvatar")]
        public async Task UpdateAvatar(UploadAvatarInput input)
        {
              await _sysUserService.UpdateAvatar(input);
        }

        /// <summary>
        /// 获取用户选择器
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/sysUser/selector")]
        public async Task<dynamic> GetUserSelector([FromQuery] UserInput input)
        {
            return await _sysUserService.GetUserSelector(input);
        }

        ///// <summary>
        ///// 用户导出
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[HttpGet("/sysUser/export")]
        //public async Task<IActionResult> ExportUser([FromQuery] UserInput input)
        //{
        //    var users = await _sysUserService.GetUser(input);  

        //    var memoryStream = new MemoryStream();
        //    memoryStream.SaveAs(users);
        //    memoryStream.Seek(0, SeekOrigin.Begin);
        //    return await Task.FromResult(new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        //    {
        //        FileDownloadName = "user.xlsx"
        //    });
        //}

        /// <summary>
        /// 用户导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("import")]
        public async Task ImportUser(IFormFile file)
        {
            var path = Path.Combine(Path.GetTempPath(), $"{IdGenerator.Id}.xlsx");
            using (var stream =   System.IO.File.Create(path))
            {
                await file.CopyToAsync(stream);
            }

            //var rows = MiniExcel.Query(path); // 解析
            //foreach (var row in rows)
            //{
            //    var a = row.A;
            //    var b = row.B;
            //    // 入库等操作

            //}
        }



    }
}
